package top.kuxiao.crmserver.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.kuxiao.crmserver.pojo.AccountManager;
import top.kuxiao.crmserver.pojo.IUser;
import top.kuxiao.crmserver.pojo.SalesExecutive;
import top.kuxiao.crmserver.pojo.SysUser;
import top.kuxiao.crmserver.service.IAccountManagerService;
import top.kuxiao.crmserver.service.ISalesExecutiveService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * 保存登录信息的工具类
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-13
 */
@Component
public class CookieUtil {


    JWTUtil jwtUtil = new JWTUtil();

    @Autowired
    IAccountManagerService accountManagerService;

    @Autowired
    ISalesExecutiveService salesExecutiveService;


    public String getToken(HttpServletRequest request) {

        String token = "";

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("user".equals(cookie.getName())) {
                    token = cookie.getValue();
                }
            }
        }

        return token;
    }

    /**
     * 判断是否有勾选保存
     */
    public boolean buttonFlag(HttpServletRequest request) {
        String token = getToken(request);

        if (jwtUtil.inValid(token)) {
            return jwtUtil.buttonFlag(token);
        } else {
            return false;
        }
    }

    public Boolean ifTokenToObject(HttpServletRequest request) {
        String token = getToken(request);
        return jwtUtil.inValid(token);
    }

    public String getRole(HttpServletRequest request) {

        String token = getToken(request);

        if (jwtUtil.inValid(token)) {
            return jwtUtil.getRole(token);
        } else {
            return null;
        }

    }

    /**
     * 获取当前登录的对象的全部信息
     */
    public IUser getObject(HttpServletRequest request) {
        String token = getToken(request);

        if (jwtUtil.inValid(token)) {

            SysUser object = jwtUtil.getObject(token);

            IUser user = null;
            if ("AM".equals(object.getPower())) {

                QueryWrapper<AccountManager> accountManagerQueryWrapper = new QueryWrapper<>();
                user = accountManagerService.getOne(accountManagerQueryWrapper.eq("am_name", object.getName()));


            } else if ("SE".equals(object.getPower())) {
                QueryWrapper<SalesExecutive> salesExecutiveQueryWrapper = new QueryWrapper<>();
                user = salesExecutiveService.getOne(salesExecutiveQueryWrapper.eq("se_name", object.getName()));
            }

            return user;

        } else {
            return null;
        }
    }


    /**
     * 获取当前登录的对象
     */
    public SysUser getUer(HttpServletRequest request) {

        String token = getToken(request);

        if (jwtUtil.inValid(token)) {
            return jwtUtil.getObject(token);
        } else {
            return null;
        }

    }


}
